import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

import ElementPlus, {ElMessage} from 'element-plus'
import 'element-plus/dist/index.css'

import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import axios from "axios";

const BASE_URL = 'http://localhost:8080';
window.BASE_URL = BASE_URL;

window.axios = axios;
axios.defaults.withCredentials = true;
const app = createApp(App)
app.config.globalProperties.$axios = axios;

app.config.globalProperties.BASE_URL = BASE_URL;

axios.interceptors.response.use(
    response => response,
    error => {
        console.log(error);
        ElMessage.error(error.response.data.msg);
        if (error.response.status == 401 || error.response.status == 403){
            localStorage.removeItem('user');
            localStorage.removeItem('breadcrumb');
            window.location.reload()
        }else if(error.response.data.indexOf('login')!=-1&&error.response.status ==404){
            window.location.reload()
        }
    }
)

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.use(ElementPlus).use(store).use(router).mount('#app')

window.getUser =()=>{
    return localStorage.user? JSON.parse(localStorage.user):null;
}
//解决ResizeObserver Error
//防抖函数 它接受一个函数 fn 和一个延迟时间 delay
//通过包装 ResizeObserver 的回调函数到一个防抖（debounce）函数中，
//你减少了回调函数的执行频率，从而可能提高了应用的性能
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
        let context = this;
        let args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    }
}
//覆盖
const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
        callback = debounce(callback, 16);
        super(callback);
    }
}